﻿package src.components 
{
	/**
	* 
	* Reliability project
	* -----------------------
	* @author  Dmitrij Boycov
	* @mail    dmitry.boycov@gmail.com
	* @date    April 27, 2010
	* @version 1
	* 
	*/
	
	// components
	import fl.controls.Button;
	import fl.controls.CheckBox;
	import fl.controls.ComboBox;
	import fl.controls.TextArea;
	import fl.controls.TextInput;
	import fl.data.DataProvider;	
	//	standart
	import flash.display.SimpleButton;
	import flash.text.TextFormat;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.Event;
	import flash.utils.Dictionary;
	//	astra
	import com.yahoo.astra.fl.containers.Form;
	import com.yahoo.astra.containers.formClasses.FormItem;
	import com.yahoo.astra.containers.formClasses.FormLayoutStyle;
	import com.yahoo.astra.events.FormDataManagerEvent;		
	import com.yahoo.astra.fl.utils.FlValueParser;
	import com.yahoo.astra.layout.LayoutContainer;
	import com.yahoo.astra.layout.modes.BoxLayout;
	import com.yahoo.astra.managers.FormDataManager;	
		
	public class BForm extends Sprite {
		
		// events
		public static const FORM_CLOSE:String = "FORM_CLOSE";
		public static const FORM_OK:String = "FORM_OK";
		//
		protected var whiteTextFormat : TextFormat;
		protected var yellowTextFormat : TextFormat;
		protected var textHeader:String;
		protected var form:Form;
		protected var formDataManager:FormDataManager;
		protected var cancelButton:Button;
		protected var okButton:Button;	
		
		protected var nameInput : TextInput;
		protected var nameFormItem:FormItem;		
		protected var description:TextArea;
		protected var descriptionFormItem:FormItem;
		
		public function BForm(textHeader:String = "") {
			super();
			this.textHeader = textHeader;
			initUI();
			initForm();
			initData();
			this.buttonMode = true;			
			this.useHandCursor = true;	
			this.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
			this.addEventListener(MouseEvent.MOUSE_UP, onUp);
			//			
		}		
		//
		private function onDown(e:MouseEvent) {
			this.startDrag();
		}		
		private function onUp(e:MouseEvent){
			this.stopDrag();
		}
		
		public function getData():Dictionary {
			var dict:Dictionary = new Dictionary();
			dict["name"] = nameInput.text;
			dict["description"] = description.text;			
			return dict;
		}
		
		public function setData(dict: Dictionary) {			
			if(dict["name"] && dict["name"]!=null)
				nameInput.text = dict["name"];
			if(dict["description"] && dict["description"]!=null)
				description.text = dict["description"];				
		}
		
		protected function createOkAndCancel() {
			// buttons
			this.okButton = new Button();
			this.okButton.addEventListener(MouseEvent.CLICK, onOk);
			this.okButton.label = "ok";
			this.okButton.setSize(40,20);
			this.cancelButton = new Button();
			this.cancelButton.label = "Cancel";
			this.cancelButton.setSize(80,20);
			this.cancelButton.addEventListener(MouseEvent.CLICK, onCancel);
		}
		/*
		* 	Create controls for forms
		*/ 
		protected function initUI() {
			nameInput = new TextInput();
			nameInput.width = 150;		
						
			description = new TextArea();			
			description.width = 150;
			description.height = 100;
			this.createOkAndCancel();			
		}		
		
		
		/**
		 * initForm
		 * Create form
		 */
		protected function initForm() : void {						
			formDataManager = new FormDataManager(FlValueParser); 
			// Init functions to be called by VALIDATION_PASSED and VALIDATION_FAILED event. 
			//formDataManager.functionValidationPassed = handlerValidationPassed;
			//formDataManager.functionValidationFailed = handlerValidationFaileded;					  
			this.form = new Form(this.textHeader);			
			this.form.formDataManager = formDataManager; 
			this.form.autoSize = true;
			this.form.paddingLeft = form.paddingRight = form.paddingTop = form.paddingBottom = 10;
			this.form.showErrorMessageText = true;
			this.form.setStyle("skin", "grayBlock");
			this.addChild(this.form);
		}		
	
		protected function getOkCancelItem() {
			return { label:"", items:[okButton, cancelButton] } ;
		}
		/*
		* 	link controls with form
		*/ 
		protected function initData() {					
			nameFormItem = new FormItem("Имя", nameInput);		
			nameFormItem.labelAlign = FormLayoutStyle.LEFT;
			nameFormItem.labelWidth = 80;
			
			descriptionFormItem = new FormItem("Описание", description);
			descriptionFormItem.labelAlign = FormLayoutStyle.LEFT;
			descriptionFormItem.labelWidth = 80;					
			
			var buttons = new FormItem("", [this.okButton, this.cancelButton]);
			buttons.labelWidth = 80;	
			
			var box : BoxLayout = new BoxLayout();
			box.direction = "vertical";
			box.verticalGap = 10;			
			var itemContainer : LayoutContainer = new LayoutContainer(box);	
			itemContainer.autoMask = false;
			itemContainer.x = itemContainer.y = 30;
						
			itemContainer.addChild(nameFormItem);			
			itemContainer.addChild(descriptionFormItem);
			itemContainer.addChild(buttons);
			this.form.addChild(itemContainer);			
		}
		
		protected function onOk(event:MouseEvent) {
			dispatchEvent(new Event(BForm.FORM_OK));	
		}		
		
		protected function onCancel(event:MouseEvent) {
			dispatchEvent(new Event(BForm.FORM_CLOSE));			
		}
		
	}	
	
}